//
//  RCMemberCardVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-8-22.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMemberCardVC.h"
#import "RCMemeberCardCell.h"
#import "RCMemberCardSuccessTakeVC.h"
#import "IManager+ModuleMemberCard.h"
#import "GloableEngineService.h"
#import "RCCardTypeInfoDataModel.h"
#import "RCBaseServerDataModel.h"
#import "NSString+NSString_Ex_.h"
#import "AppDelegate.h"

#import "RCTicketBookingVC.h"

@interface RCMemberCardVC ()<RCMemberCardTakeProtocol,IManagerDelegate>{
    int64_t _reqID;
    int64_t _reqApplyID;
}


@property (nonatomic, retain) NSMutableArray *cardIntroduceArray;
@property (nonatomic, retain) NSMutableArray *cardInfoArray;
@end

@implementation RCMemberCardVC

@synthesize cardIntroduceArray = _cardIntroduceArray;
@synthesize cardInfoArray      = _cardInfoArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.titleLabelText = @"会员卡";
    self.navigationItem.titleView = [self createTitleView];
    
    
    // 设置tableview相关参数
    if ( baseVCTableView != nil ) {
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
//        baseVCTableView.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.backgroundColor = [UIColor clearColor];
        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        baseVCTableView.frame = self.view.bounds;
        NSArray *viewControllersArray = [self.navigationController viewControllers];
        if (viewControllersArray && [viewControllersArray count] <= 1){
            CGRect tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y,baseVCTableView.frame.size.width,UIScreenHeight-20-49-44-4);
            baseVCTableView.frame = tableViewNewFrame;
        }else{
            CGRect tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y,baseVCTableView.frame.size.width,UIScreenHeight-20-44-4);
            baseVCTableView.frame = tableViewNewFrame;
        }
        
        baseVCTableView.tableHeaderView = nil;
        baseVCTableView.tableFooterView = nil;
//        [self addPullRefresh];
    }
    
    
}

- (void) viewWillAppear:(BOOL)animated
{
    NSString *cityID = [[GloableEngineService sharedInstance] selectedCityID];

    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    
    _reqID = [[IManager sharedInstance] requestMerchantCardTypeInfo:cityID WithDelegte:self];

}

- (void) dealloc
{
    [_cardInfoArray release];
    [_cardIntroduceArray release];
    [super dealloc];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UITableView Datasource & Delegate

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    int i = 0;
    if (self.cardInfoArray != nil) i++;
    if (self.cardIntroduceArray != nil) i++;
    return i;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 0) {
        return [self.cardIntroduceArray count];
    }else{
        return [self.cardInfoArray count];
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section] == 0) {
        RCCardTypeIntroduce *introduce = [self.cardIntroduceArray objectAtIndex:[indexPath row]];
        return [RCMemeberCardCell cellHeightFor:introduce];
        
    }else if ([indexPath section] == 1){
        return 90;
    }else{
        return 40;
    }
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section] == 0) {
        RCMemeberCardCell *cell = [[[RCMemeberCardCell alloc] init] autorelease];
        RCCardTypeIntroduce *introduce = [self.cardIntroduceArray objectAtIndex:[indexPath row]];
        
        [cell bindData:introduce];
        return cell;
    }else{
        RCCardTypeInfo *info = [self.cardInfoArray objectAtIndex:[indexPath row]];
        
        RCMemeberCardTypeCell *cell = [[[RCMemeberCardTypeCell alloc] init] autorelease];
        cell.delegate = self;
        [cell bindData:info];
        
        return cell;
        
    }
    
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    RCTicketBookingVC *vc = [[RCTicketBookingVC alloc] initWithType:TicketType_Train];
//    
//    //    RCMemberCardSuccessTakeVC *vc = [[RCMemberCardSuccessTakeVC alloc] init];
//    vc.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:vc animated:YES];
//    [vc release];
    
}

#pragma 
#pragma mark RCMemberCardTakeProtocol Implement
- (void) clickToTakeCard:(NSInteger)tagNumber
{
    
    NSString *bduss = [[GloableEngineService sharedInstance] bduss];
    if (![[GloableEngineService sharedInstance] hasLogin] || [NSString stringIsNullOrEmpty:bduss]) {
         [RCAppDelegate alertWithCustomizeString:@"请先登录"];
        return;
    }
   _reqApplyID =  [[IManager sharedInstance] requestApplyforCard:bduss withCardID:tagNumber WithDelegte:self];
//    RCTicketBookingVC *vc = [[RCTicketBookingVC alloc] initWithType:TicketType_Air];
//
////    RCMemberCardSuccessTakeVC *vc = [[RCMemberCardSuccessTakeVC alloc] init];
//    vc.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:vc animated:YES];
//    [vc release];
}


- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if ([dataModel isKindOfClass:[RCCardTypeInfoDataModel class]]) {
        RCCardTypeInfoDataModel *model = (RCCardTypeInfoDataModel *)dataModel;
        self.cardInfoArray = model.cardTypeInfoArray;
        self.cardIntroduceArray = model.cardIntroduceArray;
        [baseVCTableView reloadData];
    }else{
        RCMemberCardSuccessTakeVC *vc = [[RCMemberCardSuccessTakeVC alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];

    }
    
}
@end
